import { Module } from 'vuex'
import { ISystemState } from './types'
import { IRootState } from '../../types'
import { getPageListData, deletePageData, createPageData, editPageData } from '@/service/main/system/system'
const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCounter: 0,
      roleList: [],
      roleCounter: 0,
      goodsList: [],
      goodsCounter: 0,
      menuList: [],
      menuCounter: 0,
      departmentList: [],
      departmentCounter: 0,
      categoryList: [],
      categoryCounter: 0,
      storyList: [],
      storyCounter: 0
    }
  },
  mutations: {
    changeUsersList(state, usersList: any[]) {
      state.usersList = usersList
    },
    changeUsersCount(state, usersCounter: number) {
      state.usersCounter = usersCounter
    },
    changeRoleList(state, roleList: any[]) {
      state.roleList = roleList
    },
    changeRoleCount(state, roleCounter: number) {
      state.roleCounter = roleCounter
    },
    changeGoodsList(state, roleList: any[]) {
      state.goodsList = roleList
    },
    changeGoodsCount(state, roleCounter: number) {
      state.goodsCounter = roleCounter
    },
    changeMenuList(state, menuList: any[]) {
      state.menuList = menuList
    },
    changeMenuCount(state, menuCounter: number) {
      state.menuCounter = menuCounter
    },
    changeDepartmentList(state, departmentList: any[]) {
      state.departmentList = departmentList
    },
    changeDepartmentCount(state, departmentCounter: number) {
      state.departmentCounter = departmentCounter
    },
    changeCategoryList(state, categoryList: any[]) {
      state.categoryList = categoryList
    },
    changeCategoryCount(state, categoryCounter: number) {
      state.categoryCounter = categoryCounter
    },
    changeStoryList(state, storyList: any[]) {
      state.storyList = storyList
    },
    changeStoryCount(state, storyCounter: number) {
      state.storyCounter = storyCounter
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
        // switch (pageName) {
        //   case 'users':
        //     return state.usersList
        //   case 'role':
        //     return state.roleList
        // }
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Counter`]
      }
    }
  },
  actions: {
    // 获取列表数据
    async getPageListAction({ commit }, payload: any) {
      const pageName = payload.pageName
      const pageUrl = `/${pageName}/list`
      // switch (pageName) {
      //   case 'users':
      //     pageUrl = '/users/list'
      //     break
      //   case 'role':
      //     pageUrl = '/role/list'
      //     break
      // }
      const pageResult = await getPageListData(pageUrl, payload.query)
      const { list, totalCount } = pageResult.data
      const changePageName = pageName.slice(0, 1).toUpperCase() + pageName.slice(1)
      commit(`change${changePageName}List`, list)
      commit(`change${changePageName}Count`, totalCount)
      // switch (pageName) {
      //   case 'users':
      //     commit(`changeUsersList`, list)
      //     commit(`changeUsersCount`, totalCount)
      //     breakCa
      //   case 'role':
      //     commit(`changeRoleList`, list)
      //     commit(`changeRoleCount`, totalCount)
      //     break
      // }
    },
    // 删除
    async deletePageDataAction({ dispatch }, payload: any) {
      // 获取pageName和id
      const { pageName, id } = payload
      // 拼接url
      const pageUrl = `/${pageName}/${id}`
      await deletePageData(pageUrl)
      // 重新获取数据
      dispatch('getPageListAction', {
        pageName,
        query: {
          offset: 0,
          size: 10
        }
      })
    },
    // 新增
    async createPageDataAction({ dispatch }, payload: any) {
      const { pageName, newData } = payload
      const pageUrl = `/${pageName}`
      await createPageData(pageUrl, newData)
      // 重新获取数据
      dispatch('getPageListAction', {
        pageName,
        query: {
          offset: 0,
          size: 10
        }
      })
    },
    // 编辑
    async editPageDataAction({ dispatch }, payload: any) {
      const { pageName, editData, id } = payload
      const pageUrl = `/${pageName}/${id}`
      await editPageData(pageUrl, editData)
      // 重新获取数据
      dispatch('getPageListAction', {
        pageName,
        query: {
          offset: 0,
          size: 10
        }
      })
    }
  }
}

export default systemModule
